<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reset Password</title>
    <style>
        body {
            background-image: url('./login_bg1.png'); /* 替换为实际背景图像的路径 */
            background-size: cover;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .container {
            background-color: rgba(255, 255, 255, 0.8);
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            text-align: center;
            max-width: 400px;
            width: 100%;
        }
        .container h2 {
            margin-bottom: 20px;
        }
        .container input {
            width: 90%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        .container button {
            width: 90%;
            padding: 10px 0;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            margin-top: 20px;
        }
        .container button:hover {
            background-color: #45a049;
        }
        .message, .error {
            margin-top: 20px;
            display: none;
        }
        .message {
            color: green;
        }
        .error {
            color: red;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>重置密码</h2>
        <input type="password" id="newPassword" placeholder="新密码">
        <input type="password" id="confirmPassword" placeholder="确认新密码">
        <button onclick="resetPassword()">提交</button>
        <div id="message" class="message" style="display:none;">密码重置成功！</div>
        <div id="error" class="error" style="display:none;"></div>
    </div>

    <script>
        function getQueryParams() {
            const params = {};
            const queryString = window.location.search.substring(1);
            const regex = /([^&=]+)=([^&]*)/g;
            let m;
            while (m = regex.exec(queryString)) {
                params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
            }
            return params;
        }

        function resetPassword() {
            const newPassword = document.getElementById('newPassword').value;
            const confirmPassword = document.getElementById('confirmPassword').value;
            const messageElement = document.getElementById('message');
            const errorElement = document.getElementById('error');

            if (newPassword == "" || confirmPassword == "" ){
                errorElement.style.display = 'block';
                errorElement.textContent = '不允许为空！';
                return;
            }

            if (newPassword !== confirmPassword) {
                errorElement.style.display = 'block';
                errorElement.textContent = '两次密码不匹配！';
                return;
            }

            const params = getQueryParams();
            const token = params['token'];
            if (!token) {
                errorElement.style.display = 'block';
                errorElement.textContent = '链接已过期！';
                return;
            }

            fetch('http://192.168.31.61:7801/reset-password', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ token: token, newPassword: newPassword })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success && data.data == "200") {
                    messageElement.style.display = 'block';
                    errorElement.style.display = 'none';
                } else  if(data.success && data.data == "400"){	
		    messageElement.style.display = 'none';
                    errorElement.style.display = 'block';
                    errorElement.textContent = '密码重置失败！';
                }else{
			messageElement.style.display = 'none';
			errorElement.style.display = 'block';
                	errorElement.textContent = '该链接已失效！';
		}
            })
            .catch(error => {
                errorElement.style.display = 'block';
                errorElement.textContent = '出现错误： 网络异常！';
            });
        }
    </script>
</body>
</html>
